﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;

namespace LogonDesktopTools
{
    public partial class ToolsForm : Form
    {
        Properties.Settings settings = new Properties.Settings();

        public ToolsForm()
        {
            InitializeComponent();
        }

        ToolStripSplitButton Add()
        {
            ToolStripSplitButton button = new ToolStripSplitButton();
            button.DisplayStyle = ToolStripItemDisplayStyle.Text;
            button.TextAlign = ContentAlignment.MiddleCenter;
            button.Click += new EventHandler(button_select_application_Click);
            button.Text = "+";
            toolStrip.Items.Add(button);
            return button;
        }

        void Set(ToolStripSplitButton button, string application)
        {
            if (File.Exists(application))
            {
                button.DisplayStyle = ToolStripItemDisplayStyle.Image;
                button.ImageAlign = ContentAlignment.MiddleCenter;
                Icon icon = Icon.ExtractAssociatedIcon(application);
                button.Image = icon.ToBitmap();
                button.Text = application;
                ToolStripMenuItem remove_menu_item = new ToolStripMenuItem();
                remove_menu_item.DisplayStyle = ToolStripItemDisplayStyle.Text;
                remove_menu_item.Text = "-";
                button.DropDown.Items.Add(remove_menu_item);
                remove_menu_item.Click += new EventHandler(remove_menu_item_Click);
                button.Click -= button_select_application_Click;
                button.Click += new EventHandler(button_execute_application_Click);
            }
        }

        void Add(string application)
        {
            Set(Add(), application);
        }

        private void ToolsForm_Load(object sender, EventArgs e)
        {
            if (null == settings.Tools || settings.Tools.Count < 1)
            {
                settings.Tools = new System.Collections.Specialized.StringCollection();
                string taskmgr = Environment.SystemDirectory + Path.DirectorySeparatorChar + "taskmgr.exe";
                Add(taskmgr);
                settings.Tools.Add(taskmgr);
            }
            else
            {
                foreach (string application in settings.Tools)
                    Add(application);
            }
            Add();
        }

        void button_select_application_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == openFileDialog.ShowDialog(this) && !settings.Tools.Contains(openFileDialog.FileName))
            {
                Set(sender as ToolStripSplitButton, openFileDialog.FileName);
                Add();
                settings.Tools.Add(openFileDialog.FileName);
                settings.Save();
            }
        }

        void button_execute_application_Click(object sender, EventArgs e)
        {
            ToolStripSplitButton button = sender as ToolStripSplitButton;
            WinApi.STARTUPINFO startup_info = new WinApi.STARTUPINFO(true);
            startup_info.dwFlags = WinApi.STARTF_USESHOWWINDOW;
            startup_info.wShowWindow = WinApi.SW_NORMAL;
            startup_info.lpDesktop = Program.DesktopName;
            WinApi.PROCESS_INFORMATION process_info = new WinApi.PROCESS_INFORMATION(true);
            WinApi.CreateProcess(button.Text, null, IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref startup_info, ref process_info);
        }

        void remove_menu_item_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem remove_menu_item = sender as ToolStripMenuItem;
            ToolStripSplitButton button = remove_menu_item.OwnerItem as ToolStripSplitButton;
            toolStrip.Items.Remove(button);
            settings.Tools.Remove(button.Text);
            settings.Save();
        }

        private void ToolsForm_Key(object sender, KeyEventArgs e)
        {
            if (!WinApi.SetForegroundWindow(Program.DesktopForegroundWindow))
                Close();
        }
    }
}
